﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoBackupDatabase.DBConection;
using AutoBackupDatabase.ENT;
using System.Data.SqlClient;
using System.Data;

namespace AutoBackupDatabase.DAL
{
    public class DETAILGROUPUSERROLEDAL
    {
         private DbConnection conn;
         public DETAILGROUPUSERROLEDAL()
        {
            conn = new DbConnection();
        }
         public bool INSERTDETAILGROUPUSERROLE(DETAILGROUPUSERROLEENT ent)
         {
             string query = "exec  INSERTDETAILGROUPUSERROLE @GROUPUSERCODE,@ROLECODE, @PERMIT";
             SqlParameter[] sqlParameters = new SqlParameter[3];
             sqlParameters[0] = new SqlParameter("@GROUPUSERCODE", SqlDbType.Int);
             sqlParameters[0].Value = ent.GROUPUSERCODE;
             sqlParameters[1] = new SqlParameter("@ROLECODE", SqlDbType.NVarChar);
             sqlParameters[1].Value = Convert.ToString(ent.ROLECODE);
             sqlParameters[2] = new SqlParameter("@PERMIT", SqlDbType.Bit);
             sqlParameters[2].Value = Convert.ToString(ent.PERMIT);
             return conn.executeInsertQuery(query, sqlParameters);
         }
         public bool DELTELEDETAIL_GROUPUSER_ROLE(DETAILGROUPUSERROLEENT ent)
         {
             string query = "exec  DELTELEDETAIL_GROUPUSER_ROLE @GROUPUSERCODE";
             SqlParameter[] sqlParameters = new SqlParameter[1];
             sqlParameters[0] = new SqlParameter("@GROUPUSERCODE", SqlDbType.Int);
             sqlParameters[0].Value = ent.GROUPUSERCODE;
             return conn.executeDeleteQuery(query, sqlParameters);
         }
         public List<DETAILGROUPUSERROLEENT> LOADROLEBYGROUPUSERCODE(int groupUserCode)
         {
             List<DETAILGROUPUSERROLEENT> List = new List<DETAILGROUPUSERROLEENT>();
             try
             {
                 string query = string.Format("exec LOADROLEBYGROUPUSERCODE @GROUPUSERCODE");

                 SqlParameter[] sqlParameters = new SqlParameter[1];
                 sqlParameters[0] = new SqlParameter("@GROUPUSERCODE", SqlDbType.Int);
                 sqlParameters[0].Value = groupUserCode;

                 DataTable ds = conn.executeSelectQuery(query, sqlParameters);

                 DETAILGROUPUSERROLEENT departmentent;
                 foreach (DataRow item in ds.Rows)
                 {
                     departmentent = new DETAILGROUPUSERROLEENT();
                     departmentent.ROLECODE = item["ROLECODE"].ToString();
                     departmentent.GROUPUSERCODE = int.Parse(item["GROUPUSERCODE"].ToString());
                     departmentent.PERMIT = bool.Parse(item["PERMIT"].ToString());
                     departmentent.ROLENAME = item["ROLENAME"].ToString();
                     departmentent.GROUPROLENAME = item["GROUPROLENAME"].ToString();
                     List.Add(departmentent);
                 }
                 return List;
             }
             catch (Exception)
             {

                 return List;
             }



         }

         public bool DELETEDETAILROLEBYGROUPUSERID(int groupUserCode)
         {
             string query = "exec  DELETEDETAILROLEBYGROUPUSERID @GROUPUSERCODE";
             SqlParameter[] sqlParameters = new SqlParameter[1];
             sqlParameters[0] = new SqlParameter("@GROUPUSERCODE", SqlDbType.Int);
             sqlParameters[0].Value = Convert.ToString(groupUserCode);
             return conn.executeDeleteQuery(query, sqlParameters);
         }
         public bool CHECKROLE(int groupUserCode, string roleCode)
         {
             try
             {
                 string query = string.Format("exec CHECKROLE @GROUPUSERCODE,@ROLECODE");

                 SqlParameter[] sqlParameters = new SqlParameter[2];
                 sqlParameters[0] = new SqlParameter("@GROUPUSERCODE", SqlDbType.Int);
                 sqlParameters[0].Value = groupUserCode;
                 sqlParameters[1] = new SqlParameter("@ROLECODE", SqlDbType.VarChar);
                 sqlParameters[1].Value = roleCode;

                 DataTable ds = conn.executeSelectQuery(query, sqlParameters);
                 return bool.Parse(ds.Rows[0]["PERMIT"].ToString());
                 
             }
             catch (Exception)
             {

                 return false;
             }



         }
    }
}
